package sw.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sw.dao.ProjectDao;
import sw.dao.RiskLevelDao;
import sw.dao.UserDao;
import sw.dao.UserGroupDao;
import sw.entity.Group;
import sw.entity.Project;
import sw.entity.RiskLevel;
import sw.entity.UserGroup;
import sw.model.MProject;
import sw.model.MUserGroup;
import sw.model.MUserGroupProject;
import sw.service.IGroupAuthManger;
import sw.service.IProjectService;

@Service
public class ProjectServiceImpl implements IProjectService {

	@Autowired
	private ProjectDao projectDao;
	
	@Autowired
	private UserGroupDao userGroupDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RiskLevelDao riskLevelDao;
	
	@Autowired
	private IGroupAuthManger groupAuthManger;

	@Override
	public List<Project> findCreateProjectByUserId(Integer id) {
		if(null == id) return null;
		return projectDao.findByUserId(id);
	}
	
	@Override
	public Collection<MUserGroupProject> findJoinProjectByUserId(Integer userId) {
		if(null == userId) return null;
		List<UserGroup> userGroups = userGroupDao.findByUserId(userId);
		Map<Integer,MUserGroupProject> joinProjectList=new HashMap<Integer,MUserGroupProject>();
		for(UserGroup userGroup :userGroups){
			MUserGroupProject model ;
			Project pro = userGroup.getProject();
			Group group = userGroup.getGroup();
			if(joinProjectList.containsKey(pro.getId())){
				model = joinProjectList.get(pro.getId());
				model.setGroupName(model.getGroupName()+","+group.getName());
				model.setGroupIds(model.getGroupIds() + ","+group.getId());
			}else{
				model = new MUserGroupProject();
				model.setProjectId(pro.getId());
				model.setProjectName(pro.getName());
				model.setCreateDate(pro.getCreateDate());
				model.setGroupName(group.getName());
				model.setCreateUserName(pro.getUser().getName());
				model.setGroupIds(group.getId()+"");
			}
			joinProjectList.put(pro.getId(), model);		
		}
		return joinProjectList.values();
	}
	
	
	public  MProject getProject(Integer userId, Integer projectId) {
		MProject mProject = new MProject();
		Project pro = findById(projectId);
		mProject.setProject(pro);
		if(userId.equals(pro.getId())){
			  List<MUserGroup> group = groupAuthManger.findAllGroups(projectId);
			System.out.println(group.toString());
			mProject.setUserGroupList( group);
		}
		return mProject;
	}
	
	
	@Override
	public Project findById(Integer id) {
		if(null == id) return null;
		return projectDao.findOne(id);
	}
	@Override
	public MProject save(MProject model) {
		//保存项目信息（包括依赖文件）
		Project project = model.getProject();
		project.setCreateDate(new Date());
		Project pro = projectDao.save(project);
		//保存人员信息
		saveUserGroup(model.getUserGroupList(), pro);
		//保存风险等级
		savelevelList(model.getLevelList(),pro);
		return model;
	}
	
	/**
	 * 保存风险等级信息
	 * @param levelList
	 * @param pro
	 */
	private void savelevelList(List<RiskLevel> levelList,Project pro){
		if(null == levelList || levelList.size()==0) return;
		for(RiskLevel level:levelList){
			level.setProject(pro);
		}
		riskLevelDao.save(levelList);
	}
	
	/**
	 * 保存人员信息
	 * @param userGroupList
	 * @param pro
	 */
	private void saveUserGroup(List<MUserGroup> userGroupList,Project pro){
		if(null == userGroupList || userGroupList.size()==0) return;
		List<UserGroup> uList = new ArrayList<UserGroup>();
		for(MUserGroup group:userGroupList){
			Integer headManId = group.getHeadManId();//组长编号
			String[] userIds = group.getUserIds().split(",");
			
			UserGroup userGroup = new UserGroup();//添加组长
			userGroup.setProject(pro);
			userGroup.setUser(userDao.findOne(headManId));
			userGroup.setIsHeadman(1);
			uList.add(userGroup);
			
			for(String userId : userIds){//添加组长
				UserGroup uGroup = new UserGroup();//添加组长
				uGroup.setProject(pro);
				uGroup.setUser(userDao.findOne(Integer.parseInt(userId)));
				uGroup.setIsHeadman(0);
				uList.add(uGroup);
			}	
		}
		
		userGroupDao.save(uList);
	}
	
	
	public int delete(Integer id) {
		if(null == id) return 0;
	    try {
			projectDao.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}	
	
	@Override
	public Integer setImportance(Integer id, Float importance) {
		if(null == id) return null;
		Project project = projectDao.findOne(id);
		project.setImpStandard(importance);
		projectDao.save(project);
		return 0;
	}
	
	@Override
	public Project update(Project project) {
		Project pro = projectDao.save(project);
		return pro;
	}
	
	@Override
	public Project save(Project project) {
		project.setUser(userDao.findOne(1));
		project.setId(5);
		project.setImpStandard(0.0f);
		Project pro = projectDao.save(project);
		return pro;
	}
	
	
	

}
